﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Sharp.Portal.ExtNet.FormField
{
    /// <summary>
    /// 树形下拉菜单
    /// </summary>
    public class TreeListField : ElementField
    {
        /// <summary>
        /// 树形下拉菜单
        /// </summary>
        /// <param name="fieldName"></param>
        /// <param name="dataSourceName"></param>
        /// <param name="displayField"></param>
        /// <param name="valueField"></param>
        public TreeListField(string fieldName, string dataSourceName, string displayField, string valueField)
        {
            this.FieldName = fieldName;
            this.DataSourceName = dataSourceName;
            this.DisplayField = displayField;
            this.ValueField = valueField;
        }

        /// <summary>
        /// 数据源名称
        /// </summary>
        public string DataSourceName { get; set; }
        /// <summary>
        /// 显示字段名称
        /// </summary>
        public string DisplayField { get; set; }
        /// <summary>
        /// 值字段名称
        /// </summary>
        public string ValueField { get; set; }

        /// <summary>
        /// 创建
        /// </summary>
        /// <param name="nonFirst"></param>
        /// <returns></returns>
        public override string Build(bool nonFirst = false)
        {
            if (this.IsBrowsing() && !this.KeepingXType)
            {
                return string.Format("x.DisplayFieldFor(m => m.{0}).NodeId2NodeText(Model.{1}, Convert.ToString(Model.{0}))", this.FieldName, DataSourceName) + base.Build(nonFirst);
            }
            AddFieldProperty("SubmitValue", false);
            AddFieldProperty("ValueHiddenName", $"{this.FieldName}");
            AddFieldProperty("Name", $"{this.FieldName}DropDownField");

            base.AutoId = false;
            return string.Format("x.DropDownFieldFor(m => m.{0}).TreeList(Model.{1},Model.{0},\"{2}\")",
                       this.FieldName, DataSourceName, ValueField)
                       + base.Build(nonFirst)
                       + string.Format("\r\n,x.HiddenFor(m => m.{0}).AutoId()", this.FieldName);
        }
    }
}